package com.shundr.auth.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.shundr.auth.R;
import com.shundr.auth.base.BaseActivity;
import com.shundr.auth.common.view.DialogAuthConfirm;
import com.shundr.auth.common.view.DialogAuthConfirm.ConfirmedListener;
import com.shundr.auth.frame.http.LHttpRequest;
import com.shundr.auth.frame.util.StringUtil;
import com.shundr.auth.frame.util.ToastHelper;
import com.shundr.auth.user.http.UserRequest;
import com.shundr.auth.user.model.AuthOwnnerDetailInfo;

public class AuthOwnnerDetailActivity extends BaseActivity implements
		OnClickListener {

	private EditText etName;
	private EditText etID;

	private ImageView btnID;

	private int userId;

	private ImageLoader mImageLoader;
	private DisplayImageOptions mOptions;

	private AuthOwnnerDetailInfo mInfo;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case UserRequest.GET_AUTH_OWNNER_DETAIL_SUCCESSED:
				mInfo = (AuthOwnnerDetailInfo) msg.obj;
				if (mInfo != null) {
					etName.setText(mInfo.getName());
					etID.setText(mInfo.getCitizen());

					mImageLoader.displayImage(mInfo.getCitizen_pic(), btnID);
					if (mInfo.getStatus() != 1 && mInfo.getStatus() != 3) {
						btnRight.setVisibility(View.GONE);
					} else {
						btnRight.setVisibility(View.VISIBLE);
					}
				}
				break;
			case UserRequest.AUTH_OWNNER_CONFIRM_SUCCESSED:
				ToastHelper.toastShort(mContext, msg.obj.toString());
				setResult(500);
				finish();
				break;
			case LHttpRequest.REQUEST_FAILED:
				ToastHelper.toastShort(mContext, msg.obj.toString());
				break;

			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// btnRight.setVisibility(View.VISIBLE);
		setContentView(R.layout.activity_auth_ownner);
		userId = getIntent().getIntExtra("id", 0);
		if (userId == 0) {
			finish();
		}

		etName = (EditText) findViewById(R.id.et_name);
		etID = (EditText) findViewById(R.id.et_ID);

		etName.setEnabled(false);
		etID.setEnabled(false);

		btnID = (ImageView) findViewById(R.id.btn_ID);

		btnID.setOnClickListener(this);

		btnRight.setText("认证");
		btnRight.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogAuthConfirm authConfirm = new DialogAuthConfirm(mContext);
				authConfirm.setConfirmedListener(new ConfirmedListener() {

					@Override
					public void onReject(String reason) {
						new UserRequest(mContext, mHandler).authOwnnerConfrim(
								userId, "3", reason);
					}

					@Override
					public void onConfirm(String reason) {
						new UserRequest(mContext, mHandler).authOwnnerConfrim(
								userId, "2", reason);
					}
				});
				authConfirm.show(getSupportFragmentManager(), "");
			}
		});

		this.mImageLoader = ImageLoader.getInstance();
		initOptions();

		new UserRequest(mContext, mHandler).getAuthOwnnerDetail(userId);
	}

	@Override
	protected void onDestroy() {
		releaseImageView(btnID);
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent(mContext, PreviewActivity.class);
		switch (v.getId()) {
		case R.id.btn_ID:
			if (!StringUtil.isNullVal(mInfo.getCitizen_pic())) {
				intent.putExtra("url", mInfo.getCitizen_pic());
				startActivity(intent);
			}
			break;

		default:
			break;
		}
	}

	/**
	 * 初始化图片配置参数
	 */
	private void initOptions() {
		mOptions = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.iv_register_password_display)
				// 设置图片在下载期间显示的图片
				.showImageForEmptyUri(R.drawable.iv_register_password_display)
				// 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.iv_register_password_display)
				.cacheInMemory(true)
				// 设置图片加载/解码过程中错误时候显示的图片
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在内存中
				.cacheOnDisc(true)
				// 设置下载的图片是否缓存在SD卡中
				.considerExifParams(true)
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
				.bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
				.considerExifParams(true)// 设置图片下载前的延迟
				.resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
				.displayer(new RoundedBitmapDisplayer(12))// 是否设置为圆角，弧度为多少
				.build();// 构建完成
	}
}
